package com.java2104.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
//定义拦截器，需要实现HandlerInterceptor
public class SystemTimingInterceptor implements HandlerInterceptor{
    private int openTiming=8;//早晨八点开放
    private int closeTiming=18;//晚上六点关闭

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        Calendar cal=Calendar.getInstance();//当前系统时间
        int hour=cal.get(Calendar.HOUR_OF_DAY);//获取当前是几点
        if (hour>openTiming&&hour<closeTiming){
            return true;
        }else {
            response.sendRedirect("/notInWorkTime.jsp");
            return false;
        }
    }

    public int getOpenTiming() {
        return openTiming;
    }

    public void setOpenTiming(int openTiming) {
        this.openTiming = openTiming;
    }

    public int getCloseTiming() {
        return closeTiming;
    }

    public void setCloseTiming(int closeTiming) {
        this.closeTiming = closeTiming;
    }
}
